require(['jquery',
         'global',
         'module/util',
         'bootbox.min',
         'module/datatables',
         'bootstrap-datetimepicker.zh-CN',
         'bootstrap-datetimepicker',
         'module/ajax',
         'jquery-weui',
         'bootstrap',
         'pixeladmin'], 
function($, global, util, bootbox, datatables){
	window.PixelAdmin.start();
	
	var _isbnTable = null, _dictConfig = ["ISBN_MANAGE_STATUS", "ISBN_TREATMENT_OPINION", "ISBN_DISTRIBUTE_TYPE", "ISBN_DISTRIBUTE_STATUS"];
	var selectConfig = function() {
		doLoadSelect('status', 'ISBN_MANAGE_STATUS', '', '全部ISBN状态');
	}
	
	// 查询
	var doSearch = function() {
		_isbnTable.setParams(util.getSearchData('#search-area'));
		_isbnTable.invoke("fnDraw");
	};

	// 查询table的回调函数列表
	var isbnTableFunction = {
		barcode : function(data,type,full) {
			var html = '<input type="checkbox" class="element hide" id="checkall" value="'+full.id+'"/>&nbsp;&nbsp;';
			html    += '<a href="'+global.context + '/web/isbnmanage/view?barcode='+full.barcode+'" target="_blank">'+full.barcode+'</a>';
			return html;
		},
		status : function(data,type,full) {
			return global.dict['ISBN_MANAGE_STATUS'][full.status];
		},
		operate : function(data,type,full) {
			var html = '<a href="'+global.context + '/web/isbnmanage/editor?barcode='+full.barcode+'" target="_blank"><span class="glyphicon glyphicon-edit"></span>&nbsp;编辑</a>&nbsp;&nbsp;&nbsp;';
			html    += '<a href="" barcode="'+full.barcode+'"class="hide"><span class="glyphicon glyphicon-trash"></span>&nbsp;删除</a>&nbsp;&nbsp;&nbsp;';
			html += '<a href="'+global.context + '/web/isbnCirculation/index?barcode='+full.barcode+'" target="_blank" ><span class="glyphicon glyphicon-th-list"></span>&nbsp;流转记录</a>&nbsp;&nbsp;&nbsp;';
			return html;
		}
	};
	
	//表格查询后回调函数
	var tableCallBack = function(res) {
		$(".barcode").each(function() {
			$(this).click(function() {
				//return false;
			});
		});
		$(".glyphicon-trash").each(function() {
			var row = $(this).parent().parent();
			$(this).click(function() {
				$.confirm({
				  title: '确认删除？',
				  text: '你确定需要删除该记录，如需要找回请联系技术！',
				  onOK: function () {
				     deleteIsbn(this.barcode, function() {
					  	 $(row).hide(1000);
				    	 setTimeout(function(){
				    		 $(row).remove();
				    	 }, 1000);
				     });
				  },
				  onCancel: function () {
				  }
				});
				return false;
			});
		});
	};
	
	// isbn 软删除
	var deleteIsbn = function(barcode, callback) {
		$.ajax({
	        url : global.context + '/web/isbnmanage/deleteByBarcode',
	        data : {barcode : barcode},
            type : 'POST',
            dataType : 'json',
	        success : function(result) {
	        	if(callback) callback();
	        },
	        error : function(e) {
	        	$.alert("删除失败");
	        }
		});
	}
	
	// 初始化表格
	var initTable = function(){
		$('#isbnTable').css('width', "100%");
		var options = {};
		options.tableId = '#isbnTable';
		options.sAjaxSource = global.context + "/web/isbnmanage/searchByCon";
        options.functionList = isbnTableFunction;
        options.iDisplayLength = 10;
        options.callBack = tableCallBack;
        _isbnTable = datatables.init(options);
        _isbnTable.setParams(util.getSearchData("#search-area"));
		return _isbnTable.create();
	};
	
	// 初始化时间输入框
	var initDateInput = function() {
		$('#startTime').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd hh:ii:ss',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			forceParse: 0,
	        showMeridian: 1
	    });
	    $('#endTime').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd hh:ii:ss',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			forceParse: 0,
	        showMeridian: 1
	    });
	    util.bindDompareData("startTime", "endTime");// 时间比较
	}
	
	
	var doLoadSelect = function(id, dictName, firstValue, nameDesc) {
		util.selectPadding({
			id : id,
			data : global.dict[dictName],
			firstValue : firstValue,
			firstName : nameDesc
		});
	}
	
	var bindEvent = function() {
		$("#search-btn").click(doSearch);// 搜索按钮点击事件
		
		initDateInput();
		
		util.initTableCheckbox("checkall");
	}
	
	// init start
	var init  = function() {
		initTable();//初始化table
		
		global.doInitDictData({
			data : _dictConfig,
			success : function() {
				selectConfig();
				doSearch();
			}
		});
		
		bindEvent();
	};
	// init end
	
	
	init();
});